import {
	IP
} from './request.js'
const utils = {
	// 时间戳转化为标准时间
	getLocalTime(nS) {
		var date = new Date(parseInt(nS) * 1000);
		var year = date.getFullYear();
		var mon = date.getMonth() + 1;
		if (mon < 10) mon = '0' + mon
		var day = date.getDate();
		if (day < 10) day = '0' + day
		var hours = date.getHours();
		if (hours < 10) hours = '0' + hours
		var minu = date.getMinutes();
		if (minu < 10) minu = '0' + minu
		var sec = date.getSeconds();
		if (sec < 10) sec = '0' + sec
		return year + '.' + mon + '.' + day + ' ' + hours + ':' + minu + ':' + sec
	},
	getLocalTimes(nS) {
		var date = new Date(parseInt(nS) * 1000);
		var year = date.getFullYear();
		var mon = date.getMonth() + 1;
		if (mon < 10) mon = '0' + mon
		var day = date.getDate();
		if (day < 10) day = '0' + day
		var hours = date.getHours();
		if (hours < 10) hours = '0' + hours
		var minu = date.getMinutes();
		if (minu < 10) minu = '0' + minu
		var sec = date.getSeconds();
		if (sec < 10) sec = '0' + sec
		return year + '-' + mon + '-' + day + ' ' + hours + ':' + minu + ':' + sec
	},
	getdeta() {
		var date = new Date();
		var year = date.getFullYear();
		var mon = date.getMonth() + 1;
		var day = date.getDate();
		return year + '-' + mon + '-' + day
	},
	getYearMon(i) {
		var date = new Date();
		var year = date.getFullYear();
		var mon = date.getMonth() + 1;
		if (i == 1) {
			return [year, mon]
		} else {
			return year + '-' + mon;
		}

		/* 
		created() {
			let t = this.$utils.getYearMon(1)
			console.log(t);
			this.year = t[0]
			this.month = t[1]>10?t[1]:'0'+t[1]
			
			this.$api.month_transaction_details({
				year_month:this.year+'-'+this.month
			},1).then(res=>{
				console.log(res);
			})
		},
		methods:{
			handleYear(e){
				this.year = e.detail.value
			},
			handleMonth(e){
				this.month = e.detail.value.split('-')[1]
			},
		}
		 */
	},
	getThisTime() {
		var date = new Date();
		var year = date.getFullYear();
		var mon = date.getMonth() + 1;
		var day = date.getDate();
		var hours = date.getHours();
		var minu = date.getMinutes();
		var sec = date.getSeconds();
		return year + '-' + mon + '-' + day + ' ' + hours + ':' + minu + ':' + sec
	},
	forTime(obj) {
		if (obj == null) {
			return null
		}
		let date = new Date(obj);
		let m = "0" + (date.getMonth() + 1);
		let d = "0" + date.getDate();
		let h = "0" + date.getHours();
		let mm = "0" + date.getMinutes();
		return m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length) + " " + h.substring(h
			.length - 2, h.length) + ":" + mm.substring(mm.length - 2, mm.length);
	},
	formatSeconds(value) {
		var theTime = parseInt(value); // 秒
		var middle = 0; // 分
		var hour = 0; // 小时
		if (theTime > 60) {
			middle = parseInt(theTime / 60);
			theTime = parseInt(theTime % 60);
			if (middle > 60) {
				hour = parseInt(middle / 60);
				middle = parseInt(middle % 60);
			}
		}
		var result = ''
		if (parseInt(theTime) >= 10) {
			result = "0" + ":" + parseInt(theTime);
		} else {
			result = "0" + ":" + "0" + parseInt(theTime);
		}
		if (middle >= 0 && parseInt(theTime) >= 10) {
			result = parseInt(middle) + ":" + parseInt(theTime);
		} else {
			result = parseInt(middle) + ":" + "0" + parseInt(theTime);
		}
		return result

	},
	times(str) {
		let mon = new Date(str).getMonth() + 1;
		let date = new Date(str).getDate();
		return mon + '月' + date + '日'
	},
	timess(str) {
		let mon = new Date(str).getMonth() + 1;
		let date = new Date(str).getDate();
		let h = new Date(str).getHours();
		let m = new Date(str).getMinutes();
		if (mon < 10) {
			mon = '0' + mon
		}
		if (date < 10) {
			date = '0' + date
		}
		if (h < 10) {
			h = '0' + h
		}
		if (m < 10) {
			m = '0' + m
		}
		return mon + '月' + date + '日 ' + h + ':' + m
	},
	toTime(str) {
		if (str) {
			return str.substr(0, 10);
		}
		return str;
	},
	lookimg(img) {
		uni.previewImage({
			urls: [img]
		})
	},
	//银行卡加*号
	forAk(str) {
		var reg = /^(\d{4})\d+(\d{4})$/;
		return str.replace(reg, "$1 **** **** $2");
	},
	//身份证加*号
	idForAk(str) {
		var reg = /^(\d{5})\d+(\d{4})$/;
		return str.replace(reg, "$1 **** **** $2");
	},
	//手机号加*号
	phoneAk(str) {
		if (str) {
			return str.replace(/^(1[34578]\d)(\d{4})(\d{4})$/, "$1****$3");
		}
		return str;
	},
	// 拨打电话
	callphone(p) {
		uni.makePhoneCall({
			phoneNumber: p,
			complete(res) {
				console.log(res);
			}
		})
	},
	//校验价格输入验证
	Money(e) {
		return /^[0-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(e);
	},
	// 商品价格转化为保留小数点两位形式
	getprice(e) {
		e = parseInt(e);
		let re = parseInt(e / 100).toString() + '.' + (e % 100 == 0 ? "00" : e % 100);
		return re;
	},
	checkRate(input) {
		var re = /^[0-9]+.?[0-9]*/; //判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/ 
		if (!re.test(input)) {
			return false
		} else {
			return true
		}
	},
	changeTwoDecimal_f(x) { // 商品价格转化为保留小数点两位形式
		var f_x = parseFloat(x);
		if (isNaN(f_x)) {
			alert('function:changeTwoDecimal->parameter error');
			return false;
		}
		f_x = Math.round(f_x * 100) / 100;
		var s_x = f_x.toString();
		var pos_decimal = s_x.indexOf('.');
		if (pos_decimal < 0) {
			pos_decimal = s_x.length;
			s_x += '.';
		}
		while (s_x.length <= pos_decimal + 2) {
			s_x += '0';
		}
		return s_x;
	},
	// 提示弹窗
	showTips: function(msg, callback) {
		uni.showToast({
			title: msg,
			icon: 'none',
			// mask: true,
			duration: 1500,
			success: function() {
				callback && (setTimeout(function() {
					callback();
				}, 1500));
			}
		})
	},
	// 成功弹窗
	showSuccess: function(msg, callback) {
		uni.showToast({
			title: msg,
			icon: 'success',
			mask: true,
			duration: 1500,
			success: function() {
				callback && (setTimeout(function() {
					callback();
				}, 1500));
			}
		})
	},
	//判空
	isEmpty(ele) {
		if (typeof ele === 'undefined') { //先判断类型
			return true;
		} else if (ele == null) {
			return true;
		} else if (ele == '') {
			return true;
		}
		return false;
	},
	// 返回e页
	back(e) {
		uni.navigateBack({
			delta: e * -1,
			fail(err) {
				console.log(err);
			}
		})
	},
	isgologin(mch_id) {
		console.log("进入了Utils文件方法0000000000000000000000000000")
		if (mch_id) {
			console.log("请先登录00000000000000000000000000", mch_id)
			uni.showModal({
				title: '请先登陆',
				showCancel: false,
				success: function(res) {
					if (res.confirm) {
						// if (!!uni.getStorageSync("isLogin")) {
							uni.navigateTo({
								url: '/pageB/login/login?mch_id=' + mch_id
							})
						// }

					}
				}
			});
		}
		// if(!!uni.getStorageSync("isLogin")){
			uni.navigateTo({
				url: '/pageB/login/login'
			})
		// }
		
		// uni.showModal({
		// 	title: '请先登陆',
		// 	showCancel:false,
		// 	success: function (res) {
		// 		if (res.confirm) {
		// 			uni.navigateTo({
		// 				url:'/pageB/login/login'
		// 			})
		// 		}
		// 	}
		// });
	},
	//跳转页面 ,isLogin:判断是否登录
	jump(url, isLogin) {
		if (isLogin) {
			if (uni.getStorageSync('openid')) {
				uni.navigateTo({
					url: url,

				})
			} else {
				uni.navigateTo({
					url: '/pageB/login/login'
				})
			}
		} else {
			//数据成功获取，用户已登录
			uni.navigateTo({
				url: url
			})
		}
	},
	//跳转tab
	jumpTab(url, type) {
		console.log("进入JUMP跳转")
		let that = this
		if (type) {
			if (uni.getStorageSync('openid')) {
				uni.switchTab({
					url: url
				})
			} else {
				uni.showToast({
					title: '请先登录！',
					icon: "none",
					duration: 1000
				})
			}
		} else {
			//数据成功获取，用户已登录
			uni.switchTab({
				url: url
			})
		}
	},
	tips(str, duration) {
		uni.showToast({
			title: str,
			icon: "none",
			duration: duration ? duration : 1000
		})
	},
	showImage(current, urls, isList) {
		let url = [];
		if (!isList) {
			url.push(urls);
		} else {
			urls.map((item, index) => {
				url.push(item);
			});
		}
		uni.previewImage({
			current: current,
			urls: url,
			indicator: 'default',
			longPressActions: {
				success: function(data) {
					console.log(data);
				},
				fail: function(err) {
					console.log(err);
				}
			}
		});
	},
	uploads(files, success) {
		let that = this;
		uni.uploadFile({
			url: IP + '/upload/image',
			name: 'file',
			filePath: files,
			success: (res) => {
				success && success(JSON.parse(res.data));
			},
		});
	},
	// 防止处理多次点击
	noMultipleClicks(methods) {
		let that = this;
		if (that.noClick) {
			// 第一次点击
			that.noClick = false;
			methods(true);
			setTimeout(() => {
				that.noClick = true;
			}, 1000)
		} else {
			// 这里是重复点击的判断
			methods(false);
		}
	},
	// 支付
	pay(provider, orderInfo, orderID, dom, success, fail, complete) {
		let that = this;
		console.log(orderID)
		uni.requestPayment({
			provider: provider,
			orderInfo: orderInfo, //微信、支付宝订单数据 
			success(res) {
				uni.$emit('pay', {
					data: true
				})
				success && success(res.data);
			},
			fail(err) {
				console.log(err);
				uni.showToast({
					title: '支付失败',
					icon: 'none',
					duration: 2000
				});
				fail && fail(err.data);
			},
			complete(ress) {
				if (provider == 'wxpay') {
					console.log('微信回调')
					dom.$api.notifyAppWechat({
						order_no: orderID
					}).then(res => {});
				} else {
					console.log('支付宝回调')
					dom.$api.notifyAppAlipay({
						order_no: orderID
					}).then(res => {});
				}
				complete && complete(ress.data);
			},
		});
	},
};

export default utils
